<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :modal-append-to-body="false">
    <el-form :visible.sync="dialogFormVisible" :rules="rules" ref="dataForm" :model="temp" label-position="right" label-width="30%"
             style='width: 90%; margin-left:1%;'>
      <el-form-item label="父级" prop="parentId" >
        <span class="fontShow2">{{temp.show}}</span>
      </el-form-item>

      <el-form-item label="全编码展示" prop="">
        <span class="fontShow">{{applyshow}}</span>
      </el-form-item>
      <el-form-item label="编码" prop="codeNo">
        <el-input @keyup.native="filterNumber" v-model="temp.codeNo"></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="explainText">
        <el-input v-model="temp.explainText"></el-input>
      </el-form-item>

      <el-form-item label="最后一级编码" prop="codeEnd">
        <el-radio v-model="temp.codeEnd" label="0">否</el-radio>
        <el-radio v-model="temp.codeEnd" label="1">是</el-radio>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button v-if="dialogStatus=='create'" type="primary" @click="createData" id="cfmbtm1">确 定</el-button>
      <el-button v-else type="primary" @click="updateData" id="cfmbtm2">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
  import { createOne,updateOne} from '@/api/common'
  import {disableBtn, ERRS, SUCS} from '@/utils'
  import {_CodeType} from '@/api/typeConfig'

  export default {
    name: 'codeEdit',
    props: {},
    data:function () {
      return {
        dialogFormVisible:false,
        dialogStatus:'',
        textMap: {
          update: '编辑编码层级',
          create: '添加编码层级',
        },
        temp:{},
        rowdata:{},   //当前行
        rownode:{},   //当前node
        rules: {
          codeNo: [
            {required: true, message: '请输入编码', trigger: 'blur'},
            {min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur'}
          ],
          explainText: [
            {required: true, message: '请输入公司名称', trigger: 'blur'},
            {min: 1, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur'}
          ],
        },
      }
    },
    computed: {
      applyshow: function() {
        //转换undefined
        let r1= this.rowdata.fullCodeNo>=0?this.rowdata.fullCodeNo:'';
        let r2= this.temp.codeNo>=0?this.temp.codeNo:'';
        return r1+' '+r2
        // return r1+r2 +this.temp.explainText
      }
    },
    watch:{
/*      'temp.explainText':function explainText(){
        console.log(this.temp.explainText);
      }*/
    },
    methods: {
      //过滤数字以外的字符
      filterNumber() {
        this.temp.codeNo = this.temp.codeNo.toString().replace(/[^\d]/g, '')
      },
      //搜索栏 添加顶级
      handleCreate() {
        this.rowdata={};
        this.temp = {codeNo: "", explainText: "", parentId:0, codeEnd:'0',show:'顶级分类'};
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      //table操作 添加子级
      handleCreate2(node) {
        console.log(node);
        const row=node.data
        const parent=node.parent.data
        let show=`${row.level+1}级  ${row.explainText}  编码:${row.fullCodeNo}`

        this.rowdata=row; //记录自己作为父级
        this.rownode=node;
        this.temp = {
          parentId: row._id,
          codeNo: "",
          explainText: "",
          codeEnd: "0",
          show: show
        };
        this.$emit('update:tData', row)
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      //添加提交
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            disableBtn('cfmbtm1');
            this.temp.codeEnd=this.temp.codeEnd==='0'?false:true;
            if (!this.temp.parentId) {
              delete this.temp.parentId;
            }

            createOne('sparepartscode/codeExplains', this.temp).then(res => {
              this.dialogFormVisible = false
              if (res.code === 0) {
                SUCS('创建成功!')
                this.$emit('appendCode',res.data)
              } else {
                ERRS(res.message)
              }
            })

          }
        })
      },
      //详情修改
      handleUpdate(node) {
        console.log(node);
        const row=node.data
        const parent=node.parent.data
        let show=`${parent.level+1}级  ${parent.explainText}  编码:${parent.fullCodeNo}`

        this.rowdata=parent;  //记录父级
        this.rownode=node;
        this.temp = {
          id: row._id,
          codeNo: row.codeNo,
          explainText: row.explainText,
          codeEnd: _CodeType[row.codeType],
          parentId: parent._id?parent._id:'',
          show: show
        };
        if(row.level<1){
          this.temp.show=`顶级分类`;
          delete this.temp.parentId;
        }
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      //修改提交
      updateData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            disableBtn('cfmbtm2');
            this.temp.codeEnd=this.temp.codeEnd==='0'?false:true;
            delete this.temp.show;
            updateOne('sparepartscode/codeExplains', this.temp).then(res => {
              this.dialogFormVisible = false
              if (res.code === 0) {
                //更新数据
                // this.rownode.data.codeType=this.temp.codeEnd==='1'?'CODE_END':'CODE'
                // this.rownode.data.explainText=this.temp.explainText
                // this.rownode.data.codeNo=this.temp.codeNo
                this.$emit('updateCode')
                SUCS('修改成功!')
              } else {
                ERRS(res.message)
              }
            })
          }
        })
      },
    }
  }
</script>
